package com.restaurantonlineorder.seller.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.restaurantonlineorder.seller.domain.Goods;
import com.restaurantonlineorder.seller.mapper.GoodsMapper;
import com.restaurantonlineorder.seller.service.IGoodsService;
import com.restaurantonlineorder.seller.vo.req.GoodsReqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 熊宇
 * @date 2022/7/22 23:02
 * @description 商品信息 实现类
 */
@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {


    @Override
    public List<Goods> getInfo(Long storeId, Long goodsId) {
        LambdaQueryWrapper<Goods> queryWrapper = Wrappers.lambdaQuery(Goods.class);
        queryWrapper.eq(Goods::getStoreId, storeId);
        queryWrapper.eq(goodsId != null, Goods::getId, goodsId);
        List<Goods> goods = this.list(queryWrapper);
        return goods;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Goods save(GoodsReqVo goodsReqVo, HttpServletRequest request) {
        log.info("phone:{}", request.getHeader("phone"));
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsReqVo, goods);
        return this.save(goods) ? goods : null;
    }

    @Override
    public boolean addGoodsQuantity(Long goodsId, Integer quantity) {
        LambdaUpdateWrapper<Goods> updateWrapper = Wrappers.lambdaUpdate(Goods.class);
        updateWrapper.set(quantity > 0, Goods::getQuantity, quantity)
                .eq(Goods::getId, goodsId);
        return this.update(updateWrapper);
    }

   
}
